package com.cx.base.day25_Rocketmq.producer.oneWayProducer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * @Author: songyahui
 * @Date: 2020/10/4 19:24
 * @Version 1.0
 */
public class OneWayProducer {
    //    日志发送
    public static void main(String[] args) {
        //    实例化Producer
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
//    设置NameServer的地址
        producer.setNamesrvAddr("192.168.48.128:9876;192.168.48.137:9876");
//    启动Producer
        try {
            producer.start();
            for (int i = 0; i < 100; i++) {
//        创建消息，并且指定Topic,Tag和消息体
                Message msg = new Message("TopicTest", "Tag3", ("Hello Rocketmq" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                producer.sendOneway(msg);
                producer.shutdown();
            }
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
